import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPool{
    //阻塞队列用来存放任务
    private BlockingDeque<Runnable> queue=new LinkedBlockingDeque<>();

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    //此处实现一个固定线程数的线程池
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                try {
                    while (true){
                        Runnable runnable=queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}
public class demo01 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool=new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int num=i;//变量捕获只能是常量，而i是变化的
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello"+num);
                }
            });
        }
    }
}
